#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>  
#include <math.h>
#include <string.h>
#include "common.h"
#include "clientFunction.h"


#ifndef GEN_H
#include "maskGen.h"
#endif

#include "md5.h"
#include "mddriver.h"

int hashcmp(unsigned char cmp[], unsigned char hash[], char in[])
{
	int i;
	for(i = 0; i < 16; i++)
	{
		/*if(strcmp(in, "backi") == 0)
		{
			MDPrint(hash);
			printf("indice: %i cmp: %x hash: %x\n", i, cmp[i], hash[i]);
		}
		else */if(cmp[i] != hash[i])
		{
			return -1;
		}
	}
	return 0;
}

int clientFunction(job_t* job)
{
	unsigned long long JOB_NB = job->stop - job->start;
	printf("jobnb: %llu\n", JOB_NB);
	int i;
	printf("min: %i\nmax: %i\n", job->pwdMin,job->pwdMax);
	chunk(job->pwdMin,job->pwdMax,job->charsetNumber,job->start,job->stop);
	unsigned char hash[16];
	unsigned char cmp[16];
	printf("coping hash\n");
	memcpy(cmp, job->hash, 16);
	MDPrint(cmp);
	
	printf("memem %s endMEM\n", memPartagee);
	
	for(i=0;i<JOB_NB;i+=16)
	{
		char input[16];
		//printf("%s\n", input);
		memcpy(input, memPartagee+i, 16);
		MDString(input, hash);
		
		//printf("mot: %s =", input);
		//MDPrint(hash);
		if(hashcmp(cmp, hash, input) == 0)
		{
			printf("mot: %s =", input);
			MDPrint(hash);
			return wordToIndice(job->charsetNumber, input);
		}
	}
  
  return (-1);
}
